<template>
    <div class="container">
        <!-- 参数管理 -->
        <div class="paramsManageMain">
            <el-form
                :inline="true"
                :model="formData"
                label-width="110px"
                class="demo-form-inline"
                size="small"
            >
                <el-form-item label="参数类型">
                    <el-input v-model="formData.paramType" placeholder="请输入参数类型"></el-input>
                </el-form-item>
                <el-form-item label="参数名称">
                    <el-input v-model="formData.paramLabel" placeholder="请输入参数名称"></el-input>
                </el-form-item>
                <el-form-item label="参数值">
                    <el-input v-model="formData.paramValue" placeholder="请输入参数值"></el-input>
                </el-form-item>
                <!-- <el-form-item label="所属职场" v-if="flag">
                    <el-select v-model="formData.workplaceId" placeholder="请选择所属职场">
                        <el-option
                            v-for="item in selectList"
                            :key="item.id"
                            :label="item.workplaceName"
                            :value="item.id"
                        ></el-option>
                    </el-select>
                </el-form-item>-->
                <el-form-item label class="formItemBtn">
                    <el-button
                        type="primary"
                        size="small"
                        icon="el-icon-search"
                        @click="hanlesearch"
                        :loading="searchLoading"
                        class="searchBtn"
                    >查询</el-button>
                    <el-button
                        type="primary"
                        size="small"
                        icon="el-icon-refresh-right"
                        @click="handleReset"
                        :loading="resetLoading"
                        class="resetBtn"
                    >重置</el-button>
                </el-form-item>
            </el-form>
            <div style="margin-bottom: 16px">
                <el-button
                    type="primary"
                    size="small"
                    icon="el-icon-circle-plus-outline"
                    @click="hanlePush"
                >新增</el-button>
            </div>
            <el-table :data="tableData" width="100%" stripe border class="heard-bg">
                <el-table-column label="参数编码" prop="paramCode"></el-table-column>
                <el-table-column label="参数类型" prop="paramType"></el-table-column>
                <el-table-column label="参数名称" prop="paramLabel"></el-table-column>
                <el-table-column label="参数值" prop="paramValue"></el-table-column>
                <!-- <el-table-column label="所属职场" v-if="flag" prop="workplaceName"></el-table-column> -->
                <el-table-column label="备注" prop="paramDesc"></el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button
                            type="primary"
                            icon="el-icon-edit"
                            circle
                            @click="handleEdit(scope.row)"
                        />
                        <el-button
                            type="danger"
                            icon="el-icon-delete"
                            circle
                            @click="handleDelete(scope.row)"
                        />
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页 -->
            <div class="pagination">
                <Pagination
                    :hanleChangeCurrentPage="hanleChangeCurrentPage"
                    :handleChangeSize="handleChangeSize"
                    :currentPage="currentPage"
                    :pageSize="pageSize"
                    :total="total"
                />
            </div>
        </div>

        <el-dialog
            v-drag
            :close-on-click-modal="false"
            :visible.sync="Show"
            append-to-body
            center
            width="500px"
        >
            <div slot="title">{{ EditTitle }}</div>
            <el-form
                :model="Edit"
                class="menu-setting"
                label-position="right"
                ref="Edit"
                label-width="110px"
                :rules="formRules"
                size="small"
            >
                <el-form-item label="参数编码" prop="paramCode">
                    <el-input v-model="Edit.paramCode" placeholder="请输入" style="width: 200px" />
                </el-form-item>
                <el-form-item label="参数类型">
                    <el-input v-model="Edit.paramType" placeholder="请输入" style="width: 200px" />
                </el-form-item>
                <el-form-item label="参数名称">
                    <el-input v-model="Edit.paramLabel" placeholder="请输入" style="width: 200px" />
                </el-form-item>
                <el-form-item label="参数值" prop="paramValue">
                    <el-input v-model="Edit.paramValue" placeholder="请输入" style="width: 200px" />
                </el-form-item>
                <!-- <el-form-item label="所属职场" v-if="flag" prop="workplaceId">
                    <el-select
                        v-model="Edit.workplaceId"
                        placeholder="请选择所属职场"
                        style="width: 200px"
                    >
                        <el-option
                            v-for="item in selectList"
                            :key="item.id"
                            :label="item.workplaceName"
                            :value="item.id"
                        ></el-option>
                    </el-select>
                </el-form-item>-->
                <el-form-item label="备注">
                    <el-input v-model="Edit.paramDesc" placeholder="请输入" style="width: 200px" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button
                    size="small"
                    icon="el-icon-circle-close"
                    @click="Show = false"
                    class="cancelBtn"
                >取 消</el-button>
                <el-button
                    type="primary"
                    size="small"
                    @click="submitEdit"
                    class="submitBtn"
                    icon="el-icon-circle-check"
                >确定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import Pagination from '@/components/Pagination/Pagination.vue';
import { getToken, getTokenHsbankNo } from '@/utils/auth';
// const validCheckNum = function (rule, value, callback) {
//   if (value) {
//     const arr = value.split('-');
//     if (parseInt(arr[1]) <= parseInt(arr[0])) {
//       callback(new Error('区间段后面的应该比前面的大！'));
//     } else {
//       callback();
//     }
//   }
// };
export default {
    name: 'paramsManagement',
    components: {
        Pagination,
    },
    data() {
        return {
            flag: true,
            formData: {
                workplaceId: '',
                paramType: '',
                paramLabel: '',
                paramValue: '',
            },
            // 新增职场字段
            Edit: {
                workplaceId: '',
                paramCode: '',
                paramDesc: '',
                paramType: '',
                paramLabel: '',
                paramValue: '',
            },
            EditTitle: '',
            Show: false,
            // 搜索loading
            searchLoading: false,
            // 重置loading
            resetLoading: false,
            formRules: {
                workplaceId: [
                    { required: true, message: '请选择所属职场', trigger: 'change' },
                ],
                paramCode: [
                    { required: true, message: '请输入职场名称', trigger: 'blur' },
                    { max: 50, message: '字符最大长度为50', trigger: 'blur' },
                ],
                paramValue: [
                    { required: true, message: '请输入参数值', trigger: 'blur' },
                    { max: 50, message: '字符最大长度为50', trigger: 'blur' },

                ],
            },
            // 当前页码
            currentPage: 1,
            // 页码大小
            pageSize: 10,
            // 总数
            total: 0,
            // 表格内容
            tableData: [],
            // 下拉数据列表
            selectList: [],
            // 判断新增还是编辑
            ifAddOrUpdate: Boolean,
        };
    },
    mounted() {
        this.getInitData(this.formData);
        this.getSelectdata();
        if (JSON.parse(getToken()).userType === '1') {
            this.flag = false;
        }
    },
    methods: {
        // 获取参数表格数据
        getInitData(form) {
            let params = {
                pageNum: this.currentPage,
                pageSize: this.pageSize,
            };
            params = Object.assign({}, params, form);
            this.$post.Params.searchParams(params)
                .then((res) => {
                    if (res.data.success) {
                        this.tableData = res.data.content.paramList;
                        this.total = res.data.content.paramCount;
                    }
                });
        },
        // 获取职场下拉选项数据
        getSelectdata() {
            const params = {
                pageNum: 1,
                pageSize: 10,
                lesseeCode: getTokenHsbankNo('HsbankNo'),
            };
            this.$post.Workplace.searchWorkplace(params)
                .then((res) => {
                    console.log(res);
                    if (res.status == 200) {
                        this.selectList = res.data.content.workplaceList;
                    }
                });
        },
        // 点击搜索
        hanlesearch() {
            this.currentPage = 1;
            this.pageSize = 10;
            this.getInitData(this.formData);
        },
        // 点击重置
        handleReset() {
            this.formData = {
                lesseeId: '',
                paramType: '',
                paramLabel: '',
                paramValue: '',
            };
            this.currentPage = 1;
            this.pageSize = 10;
            this.getInitData(this.formData);
        },
        // 点击新增
        hanlePush() {
            this.EditTitle = '添加参数';
            this.Edit = {};
            this.Show = true;
            this.ifAddOrUpdate = true;
            // 解决一进来就进行表单验证问题
            if (this.$refs.Edit) {
                this.$refs.Edit.resetFields();
            } else {
                this.$nextTick(() => {
                    // this.$refs.Edit.resetFields();
                });
            }
        },
        // 点击编辑
        handleEdit(row) {
            this.EditTitle = '修改参数';
            this.Edit = JSON.parse(JSON.stringify(row));
            this.Show = true;
            this.ifAddOrUpdate = false;
            if (this.$refs.Edit) {
                this.$refs.Edit.resetFields();
            } else {
                this.$nextTick(() => {
                    // this.$refs.Edit.resetFields();
                });
            }
        },
        // 单个删除职场
        handleDelete(row) {
            this.$confirm('确认删除该参数？', '提示', { customClass: 'warning', type: 'warning' })
                .then(() => {
                    this.$post.Params.deleteParams({ id: row.id })
                        .then((res) => {
                            if (res.data.success) {
                                this.$message.success('删除成功！');
                                if (this.tableData.length === 1) {
                                    this.currentPage = this.currentPage - 1;
                                }
                                this.getInitData(this.formData);
                            } else {
                                this.$message.error('删除失败！');
                            }
                        });
                })
                .catch(() => {
                });
        },
        // 新增职场点击确定按钮
        submitEdit() {
            if (this.ifAddOrUpdate == false) {
                // 编辑
                this.$refs.Edit.validate((valid) => {
                    if (valid) {
                        this.$post.Params.updateParams(this.Edit)
                            .then((res) => {
                                if (res.data.success) {
                                    this.$message.success('修改成功！');
                                    this.getInitData(this.formData);
                                    this.Show = false;
                                } else {
                                    this.$message.error(res.data.msg);
                                }
                            });
                    } else {
                        this.$message.warning('请完成表单填写！');
                    }
                });
            } else {
                // 添加
                this.$refs.Edit.validate((valid) => {
                    if (valid) {
                        this.$post.Params.addParams(this.Edit)
                            .then((res) => {
                                if (res.data.success) {
                                    this.$message.success('添加成功！');
                                    this.getInitData(this.formData);
                                    this.Show = false;
                                } else {
                                    this.$message.error(res.data.msg);
                                }
                            });
                    } else {
                        this.$message.warning('请完成表单填写！');
                    }
                });
            }
        },
        // 页码切换方法
        hanleChangeCurrentPage(val) {
            this.currentPage = val;
            this.getInitData();
        },
        // 切换分页大小方法
        handleChangeSize(val) {
            this.pageSize = val;
            this.getInitData();
        },
    },
};
</script>

<style  lang="scss">
.container {
    width: 100%;
    height: 100%;
    .paramsManageMain {
        .formItemBtn {
            margin-left: 20px;
        }
        width: 100%;
        background: #fff;
        margin: 0 0 15px;
        // border-radius:5px;
        /* padding: 20px; */
        box-sizing: border-box;
        text-align: left;
        .tableTitle {
            width: 100%;
            height: 48px;
            border: 1px solid #eee;
            border-bottom: none;
            line-height: 48px;
            margin-top: 20px;
            color: #666;
        }
        .pagination {
            margin-top: 20px;
            text-align: right;
        }
    }
}
</style>
